home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / gopherlib.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  6KB  |  240 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Gopher protocol client interface.'''
  5. __all__ = [
  6.     'send_selector',
  7.     'send_query']
  8. import warnings
  9. warnings.warn('the gopherlib module is deprecated', DeprecationWarning, stacklevel = 2)
  10. DEF_SELECTOR = '1/'
  11. DEF_HOST = 'gopher.micro.umn.edu'
  12. DEF_PORT = 70
  13. A_TEXT = '0'
  14. A_MENU = '1'
  15. A_CSO = '2'
  16. A_ERROR = '3'
  17. A_MACBINHEX = '4'
  18. A_PCBINHEX = '5'
  19. A_UUENCODED = '6'
  20. A_INDEX = '7'
  21. A_TELNET = '8'
  22. A_BINARY = '9'
  23. A_DUPLICATE = '+'
  24. A_SOUND = 's'
  25. A_EVENT = 'e'
  26. A_CALENDAR = 'c'
  27. A_HTML = 'h'
  28. A_TN3270 = 'T'
  29. A_MIME = 'M'
  30. A_IMAGE = 'I'
  31. A_WHOIS = 'w'
  32. A_QUERY = 'q'
  33. A_GIF = 'g'
  34. A_HTML = 'h'
  35. A_WWW = 'w'
  36. A_PLUS_IMAGE = ':'
  37. A_PLUS_MOVIE = ';'
  38. A_PLUS_SOUND = '<'
  39. _names = dir()
  40. _type_to_name_map = { }
  41.  
  42. def type_to_name(gtype):
  43.     """Map all file types to strings; unknown types become TYPE='x'."""
  44.     if _type_to_name_map == { }:
  45.         for name in _names:
  46.             if name[:2] == 'A_':
  47.                 _type_to_name_map[eval(name)] = name[2:]
  48.                 continue
  49.         
  50.     
  51.     if gtype in _type_to_name_map:
  52.         return _type_to_name_map[gtype]
  53.     
  54.     return 'TYPE=%r' % (gtype,)
  55.  
  56. CRLF = '\r\n'
  57. TAB = '\t'
  58.  
  59. def send_selector(selector, host, port = 0):
  60.     '''Send a selector to a given host and port, return a file with the reply.'''
  61.     import socket as socket
  62.     if not port:
  63.         i = host.find(':')
  64.         if i >= 0:
  65.             host = host[:i]
  66.             port = int(host[i + 1:])
  67.         
  68.     
  69.     if not port:
  70.         port = DEF_PORT
  71.     elif type(port) == type(''):
  72.         port = int(port)
  73.     
  74.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75.     s.connect((host, port))
  76.     s.sendall(selector + CRLF)
  77.     s.shutdown(1)
  78.     return s.makefile('rb')
  79.  
  80.  
  81. def send_query(selector, query, host, port = 0):
  82.     '''Send a selector and a query string.'''
  83.     return send_selector(selector + '\t' + query, host, port)
  84.  
  85.  
  86. def path_to_selector(path):
  87.     '''Takes a path as returned by urlparse and returns the appropriate selector.'''
  88.     if path == '/':
  89.         return '/'
  90.     else:
  91.         return path[2:]
  92.  
  93.  
  94. def path_to_datatype_name(path):
  95.     '''Takes a path as returned by urlparse and maps it to a string.
  96.     See section 3.4 of RFC 1738 for details.'''
  97.     if path == '/':
  98.         return "TYPE='unknown'"
  99.     else:
  100.         return type_to_name(path[1])
  101.  
  102.  
  103. def get_directory(f):
  104.     '''Get a directory in the form of a list of entries.'''
  105.     entries = []
  106.     while None:
  107.         line = f.readline()
  108.         if not line:
  109.             print '(Unexpected EOF from server)'
  110.             break
  111.         
  112.         if line[-2:] == CRLF:
  113.             line = line[:-2]
  114.         elif line[-1:] in CRLF:
  115.             line = line[:-1]
  116.         
  117.         if line == '.':
  118.             break
  119.         
  120.         if not line:
  121.             print '(Empty line from server)'
  122.             continue
  123.         
  124.         gtype = line[0]
  125.         parts = line[1:].split(TAB)
  126.         if len(parts) < 4:
  127.             print '(Bad line from server: %r)' % (line,)
  128.             continue
  129.         
  130.         if len(parts) > 4:
  131.             if parts[4:] != [
  132.                 '+']:
  133.                 print '(Extra info from server:', parts[4:], ')'
  134.             
  135.         else:
  136.             parts.append('')
  137.         entries.append(parts)
  138.         continue
  139.         return entries
  140.  
  141.  
  142. def get_textfile(f):
  143.     '''Get a text file as a list of lines, with trailing CRLF stripped.'''
  144.     lines = []
  145.     get_alt_textfile(f, lines.append)
  146.     return lines
  147.  
  148.  
  149. def get_alt_textfile(f, func):
  150.     '''Get a text file and pass each line to a function, with trailing CRLF stripped.'''
  151.     while None:
  152.         line = f.readline()
  153.         if not line:
  154.             print '(Unexpected EOF from server)'
  155.             break
  156.         
  157.         if line[-2:] == CRLF:
  158.             line = line[:-2]
  159.         elif line[-1:] in CRLF:
  160.             line = line[:-1]
  161.         
  162.         if line == '.':
  163.             break
  164.         
  165.         if line[:2] == '..':
  166.             line = line[1:]
  167.         
  168.         continue
  169.         return None
  170.  
  171.  
  172. def get_binary(f):
  173.     '''Get a binary file as one solid data block.'''
  174.     data = f.read()
  175.     return data
  176.  
  177.  
  178. def get_alt_binary(f, func, blocksize):
  179.     '''Get a binary file and pass each block to a function.'''
  180.     while None:
  181.         data = f.read(blocksize)
  182.         if not data:
  183.             break
  184.         
  185.         continue
  186.         return None
  187.  
  188.  
  189. def test():
  190.     '''Trivial test program.'''
  191.     import sys as sys
  192.     import getopt as getopt
  193.     (opts, args) = getopt.getopt(sys.argv[1:], '')
  194.     selector = DEF_SELECTOR
  195.     type = selector[0]
  196.     host = DEF_HOST
  197.     if args:
  198.         host = args[0]
  199.         args = args[1:]
  200.     
  201.     if args:
  202.         type = args[0]
  203.         args = args[1:]
  204.         if len(type) > 1:
  205.             type = type[0]
  206.             selector = type
  207.         else:
  208.             selector = ''
  209.             if args:
  210.                 selector = args[0]
  211.                 args = args[1:]
  212.             
  213.         query = ''
  214.         if args:
  215.             query = args[0]
  216.             args = args[1:]
  217.         
  218.     
  219.     if type == A_INDEX:
  220.         f = send_query(selector, query, host)
  221.     else:
  222.         f = send_selector(selector, host)
  223.     if type == A_TEXT:
  224.         lines = get_textfile(f)
  225.         for item in lines:
  226.             print item
  227.         
  228.     elif type in (A_MENU, A_INDEX):
  229.         entries = get_directory(f)
  230.         for item in entries:
  231.             print item
  232.         
  233.     else:
  234.         data = get_binary(f)
  235.         print 'binary data:', len(data), 'bytes:', repr(data[:100])[:40]
  236.  
  237. if __name__ == '__main__':
  238.     test()
  239.  
  240.